package com.seednia.foundation.framework.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HierarchicalTask implements ITask, IHierarchical<HierarchicalTask> {

	HierarchicalTask parent = null;
	List<HierarchicalTask> children = new ArrayList<HierarchicalTask>();
	
	public void addChild(HierarchicalTask child)
	{
		children.add(child);
	}
	
	@Override
	public void initialize() {
		for (HierarchicalTask child: getChildren())
		{
			child.initialize();
		}
	}

	@Override
	public void execute(Map<String, Object> context) throws Exception {
		for (HierarchicalTask child: getChildren())
		{
			child.execute(context);
		}
	}

	@Override
	public HierarchicalTask getParent() {
		return parent;
	}

	@Override
	public void setParent(HierarchicalTask parent) {
		this.parent = parent;
	}

	@Override
	public List<HierarchicalTask> getChildren() {
		return children;
	}

	@Override
	public HierarchicalTask getChildrenAt(int index) {
		return children.get(index);
	}

}
